window.MathJax = {
    tex: {
        inlineMath: [['$', '$'], ['\\(', '\\)']],
    },
    chtml: {
        scale: 0.8
    }
};

//动态公式列表
var formulaList2018 = [
    {
        "technicalIndicator": "单位产品取水量",
        "formula": "$V_{ui} = \\frac {V_i}{Q}$",
        "label1": "$V_i$ —— 在一定计量时间内，企业用于生产该产品的取水量，单位为立方米（m³）",
        "label2": "$Q$ —— 在一定计量时间内的产品产量",
        "label3": "",
        "input1": -1,
        "input2": -1,
        "input3": -1,
    },
    {
        "technicalIndicator": "重复利用率",
        "formula": "$R = \\frac {V_r}{V_i \\ + \\ V_r} \\times 100 \\%$",
        "label1": "$V_r$ —— 在一定的计量时间内，企业的重复利用水量，单位为立方米（m³）",
        "label2": "$V_i$ —— 在一定的计量时间内，企业的取水量，单位为立方米（m³）",
        "label3": "",
        "input1": -1,
        "input2": -1,
        "input3": -1,
    },
    {
        "technicalIndicator": "直接冷却水循环率",
        "formula": "$R_d = \\frac {V_{dr}}{V_{df} \\ + \\ V_{dr}} \\times 100 \\%$",
        "label1": "$V_{dr}$ —— 直接冷却水循环量，单位为立方米每小时（m³/h）",
        "label2": "$V_{df}$ —— 直接冷却水循环系统补充水量，单位为立方米每小时（m³/h）",
        "label3": "",
        "input1": -1,
        "input2": -1,
        "input3": -1,
    },
    {
        "technicalIndicator": "间接冷却水循环率",
        "formula": "$R_c = \\frac {V_{cr}}{V_{cf} \\ + \\ V_{cr}} \\times 100 \\%$",
        "label1": "$V_{cr}$ —— 间接冷却水循环量，单位为立方米每小时（m³/h）",
        "label2": "$V_{cf}$ —— 间接冷却水循环系统补充量，单位为立方米每小时（m³/h）",
        "label3": "",
        "input1": -1,
        "input2": -1,
        "input3": -1,
    },
    {
        "technicalIndicator": "蒸汽冷凝水回用率",
        "formula": "$R_b = \\frac {V_{br}}{D} \\times \\rho \\times 100 \\%$",
        "label1": "$V_{br}$ —— 蒸汽冷凝水回用量，单位为立方米每小时（m³/h）",
        "label2": "$D$ —— 产汽设备的产汽量，单位为吨每小时（t/h）",
        "label3": "$\\rho$ —— 蒸汽体积质量，单位为吨每立方米（t/m³）",
        "input1": -1,
        "input2": -1,
        "input3": -1,
    },
    {
        "technicalIndicator": "废水回用率",
        "formula": "$K_w = \\frac {V_w}{V_d \\ + \\ V_w} \\times 100 \\%$",
        "label1": "$V_w$ —— 在一定的计量时间内，企业对外排废水自行处理后的回用水量，单位为立方米（m³）",
        "label2": "$V_d$ —— 在一定的计量时间内，企业的排水量，单位为立方米（m³）",
        "label3": "",
        "input1": -1,
        "input2": -1,
        "input3": -1,
    },
    {
        "technicalIndicator": "非常规水源替代率",
        "formula": "$K_b = \\frac {V_{ih}}{V_i \\ + \\ V_{ih}} \\times 100 \\%$",
        "label1": "$V_{ih}$ —— 在一定的计量时间内，非常规水源所替代的取水量，单位为立方米（m³）",
        "label2": "$V_i$ —— 在一定的计量时间内，企业的取水量，单位为立方米（m³）",
        "label3": "",
        "input1": -1,
        "input2": -1,
        "input3": -1,
    },
    {
        "technicalIndicator": "非常规水源利用率",
        "formula": "$K_u = \\frac {V_{iu}}{V} \\times 100 \\%$",
        "label1": "$V_{iu}$ —— 在一定的计量时间内，非常规水源利用量，单位为立方米（m³）",
        "label2": "$V$ —— 在一定的计量时间内，非常规水源总量，单位为立方米（m³）",
        "label3": "",
        "input1": -1,
        "input2": -1,
        "input3": -1,
    },
    {
        "technicalIndicator": "用水综合漏失率",
        "formula": "$K_l = \\frac {V_l}{V_i} \\times 100 \\%$",
        "label1": "$V_l$ —— 在一定的计量时间内，企业的漏失水量，单位为立方米（m³）",
        "label2": "$V_i$ —— 在一定的计量时间内，企业的取水量，单位为立方米（m³）",
        "label3": "",
        "input1": -1,
        "input2": -1,
        "input3": -1,
    },
    {
        "technicalIndicator": "达标排放率",
        "formula": "$K_d = \\frac {V_{d \\,'}}{V_d} \\times 100 \\%$",
        "label1": "$V_{d \\,'}$ —— 在一定的计量时间内，企业的达到排放标准的排水量，单位为立方米（m³）",
        "label2": "$V_d$ —— 在一定的计量时间内，企业的排水量，单位为立方米（m³）",
        "label3": "",
        "input1": -1,
        "input2": -1,
        "input3": -1,
    },
    {
        "technicalIndicator": "水表计量率",
        "formula": "$K_m = \\frac {V_{mi}}{V_i} \\times 100 \\%$",
        "label1": "$V_{mi}$ —— 在一定的计量时间内，企业或企业内各层次用水单元的水表计量的用（或取）水量，单位为立方米（m³）",
        "label2": "$V_i$ —— 在一定的计量时间内，企业或企业内各层次用水单元的用（或取）水量，单位为立方米（m³）",
        "label3": "",
        "input1": -1,
        "input2": -1,
        "input3": -1,
    },
    {
        "technicalIndicator": "化学水制取系数",
        "formula": "$k_1 = \\frac {V_{cin}}{V_{ch}}$",
        "label1": "$V_{cin}$ —— 制取化学水所用的取水量（软化水量、除盐水量折算成的取水量），单位为立方米（m³）",
        "label2": "$V_{ch}$ —— 化学水水量（软化水量、除盐水量），单位为立方米（m³）",
        "label3": "",
        "input1": -1,
        "input2": -1,
        "input3": -1,
    },
    {
        "technicalIndicator": "循环水浓缩倍数",
        "formula": "$N = \\frac {C_{cy}}{C_f}$",
        "label1": "$C_{cy}$ —— 间接冷却循环冷却水实测某离子浓度，单位为毫克每升（mg/L）",
        "label2": "$C_f$ —— 间接冷却循环系统补充水实测某离子浓度，单位为毫克每升（mg/L）",
        "label3": "",
        "input1": -1,
        "input2": -1,
        "input3": -1,
    },
    {
        "technicalIndicator": "蒸汽冷凝水回收率",
        "formula": "$R_b = \\frac {V_{br}}{D} \\times \\rho_b \\times 100 \\%$",
        "label1": "$V_{br}$ —— 在统计期内，蒸汽冷凝水回收量（应包括外供量，特指外供给有效使用不降低能损的用户），单位为立方米每小时（m³/h）",
        "label2": "$D$ —— 在统计期内，生产过程中产汽设备的产汽量+进入装置的蒸汽量-外供出装置的蒸汽量，单位为吨每小时（t/h）",
        "label3": "$\\rho_b$ —— 冷凝水体积质量，单位为吨每立方米（t/m³）",
        "input1": -1,
        "input2": -1,
        "input3": -1,
    },
    {
        "technicalIndicator": "单位产品排水量",
        "formula": "$V_{ud} = \\frac {V_d}{Q}$",
        "label1": "$V_d$ —— 在统计期内，装置的排水量，单位为立方米（m³）",
        "label2": "$Q$ —— 在统计期内，产品产量",
        "label3": "",
        "input1": -1,
        "input2": -1,
        "input3": -1,
    },
]

var formulaList2011 = [
    {
        "technicalIndicator": "棉、麻、化纤及混纺机织物",
        "formula": "$V_{ui} = \\frac {V_i}{Q}$",
        "label1": "$V_i$ —— 在统计期内，企业生产某种产品的取水量，单位为立方米(m³)",
        "label2": "$Q$ —— 统计期内，生产该产品的产量。机织印染产品，单位为百米(100 m)；针织印染产品，单位为吨(t)",
        "label3": "",
        "input1": -1,
        "input2": -1,
        "input3": -1,
    },
    {
        "technicalIndicator": "丝绸机织物",
        "formula": "$V_{ui} = \\frac {V_i}{Q}$",
        "label1": "$V_i$ —— 在统计期内，企业生产某种产品的取水量，单位为立方米(m³)",
        "label2": "$Q$ —— 统计期内，生产该产品的产量。机织印染产品，单位为百米(100 m)；针织印染产品，单位为吨(t)",
        "label3": "",
        "input1": -1,
        "input2": -1,
        "input3": -1,
    },
    {
        "technicalIndicator": "针织物及纱线",
        "formula": "$V_{ui} = \\frac {V_i}{Q}$",
        "label1": "$V_i$ —— 在统计期内，企业生产某种产品的取水量，单位为立方米(m³)",
        "label2": "$Q$ —— 统计期内，生产该产品的产量。机织印染产品，单位为百米(100 m)；针织印染产品，单位为吨(t)",
        "label3": "",
        "input1": -1,
        "input2": -1,
        "input3": -1,
    },
    {
        "technicalIndicator": "重复利用率",
        "formula": "$R = \\frac {V_r}{V_i+V_{i \\,'}+V_r} \\times 100 \\%$",
        "label1": "$V_r$ —— 在统计期内，生产过程中的重复利用水量，单位为立方米(m³)",
        "label2": "$V_i$ —— 在统计期内，生产过程中的取水量，单位为立方米(m³)",
        "label3": "$V_{i \\,'}$ —— 在统计期内，生产过程中的非常规水资源取水量，单位为立方米(m³)",
        "input1": -1,
        "input2": -1,
        "input3": -1,
    },
    {
        "technicalIndicator": "间接冷却水循环率",
        "formula": "$R_c = \\frac {V_{cr}}{V_{cr}+V_{cf}} \\times 100 \\%$",
        "label1": "$V_{cr}$ —— 间接冷却水循环量，单位为立方米每小时(m³／h)",
        "label2": "$V_{cf}$ —— 间接冷却水循环系统补充水量，单位为立方米每小时(m³／h)",
        "label3": "",
        "input1": -1,
        "input2": -1,
        "input3": -1,
    },
    {
        "technicalIndicator": "冷凝水回用率",
        "formula": "$R_b = \\frac {V_{br}}{D} \\times \\rho \\times 100 \\%$",
        "label1": "$V_{br}$ —— 蒸汽冷凝水回用量，单位为立方米每小时(m³／h)",
        "label2": "$D$ —— 产汽设备产汽量，单位为吨每小时(t／h)",
        "label3": "$\\rho$ —— 蒸汽体积质量，单位为吨每立方米(t／m³)",
        "input1": -1,
        "input2": -1,
        "input3": -1,
    },
    {
        "technicalIndicator": "废水回用率",
        "formula": "$K_w = \\frac {V_w}{V_d \\ + \\ V_w} \\times 100 \\%$",
        "label1": "$V_w$ —— 在统计期内，企业外排废水处理后的回用水量，单位为立方米(m³)",
        "label2": "$V_d$ —— 在统计期内，企业向外排放的废水量，单位为立方米(m³)",
        "label3": "",
        "input1": -1,
        "input2": -1,
        "input3": -1,
    },
    {
        "technicalIndicator": "用水综合漏失率",
        "formula": "$K_l = \\frac {V_l}{V_i} \\times 100 \\%$",
        "label1": "$V_l$ —— 在统计期内，企业的漏失水量，单位为立方米(m³)",
        "label2": "$V_i$ —— 在统计期内，企业的取水量，单位为立方米(m³)",
        "label3": "",
        "input1": -1,
        "input2": -1,
        "input3": -1,
    },
]

//管理考核指标的表头
var managementColumns = [
    {field: 'id', title: '标示符', align: 'center', valign: 'middle', colspan: 1, rowspan: 1},
    {field: 'showId', title: '序号', align: 'center', valign: 'middle', colspan: 1, rowspan: 1},
    {field: 'assessmentIndicator', title: "考核指标", align: 'center', valign: 'middle', colspan: 1, rowspan: 1,},
    {field: 'assessmentContent', title: "考核内容", align: 'center', valign: 'middle', colspan: 1, rowspan: 1},
    {field: 'assessmentMethod', title: "考核方法", align: 'center', valign: 'middle', colspan: 1, rowspan: 1},
    {field: 'fullMark', title: "满分", align: 'center', valign: 'middle', colspan: 1, rowspan: 1},
    {
        field: 'score', title: "评分", align: 'center', valign: 'middle', colspan: 1, rowspan: 1,
        editable: {
            type: 'text',
            title: '评分',
            //disabled: false,             //是否禁用编辑
            emptytext: "编辑",
            validate: function (value) {
                if (parseFloat(value) != value) {
                    return "请输入数字";
                }
            }
        },
        formatter: function (value, row, index) {
            if (value == 'Empty' || value == '' || value == null) {
                return "";
            } else {
                return value;
            }
        }
    },
    {
        field: 'totalScore', title: "合计", align: 'center', valign: 'middle', colspan: 1, rowspan: 1,
        formatter: function (value, row, index) {
            if (value == null || value == '') {
                return 0;
            } else {
                return value;
            }
        },
    },
]

//技术考核指标的表头
var technicalColumns = [
    {field: 'id', title: '标示符', align: 'center', valign: 'middle', colspan: 1, rowspan: 1},
    {title: '序号', field: 'showId', align: 'center', valign: 'middle', colspan: 1, rowspan: 1},
    {field: 'assessmentContent', title: "考核内容", align: 'center', valign: 'middle', colspan: 1, rowspan: 1},
    {field: 'technicalIndicator', title: "技术指标", align: 'center', valign: 'middle', colspan: 1, rowspan: 1},
    {field: 'unit', title: "单位", align: 'center', valign: 'middle', colspan: 1, rowspan: 1},
    {
        field: 'value', title: "计算值", align: 'center', valign: 'middle', colspan: 1, rowspan: 1,
        formatter: function (value, row, index) {
            if (operation == 'edit') {
                if (value == null || value == '') {
                    var e = '<a onclick="edit(\'' + index + '\', \'' + row.technicalIndicator + '\')"><i class="fa fa-edit" ></i>编辑</a>';
                    return e;
                } else {
                    var e = '<a onclick="edit(\'' + index + '\', \'' + row.technicalIndicator + '\')">' + value + '</a>';
                    return e;
                }
            } else {
                if (value == null || value == '') {
                    return '';
                } else {
                    return value;
                }
            }

        }
    },
    {field: 'assessmentValue', title: "考核值", align: 'center', valign: 'middle', colspan: 1, rowspan: 1},
]

var ManagementTableInit = function () {
    var oTableInit = new Object();
    //初始化Table
    oTableInit.Init = function () {
        var tableName = document.getElementById("management_table").createCaption();
        tableName.innerHTML = "<b>管理考核指标</b>";
        $('#management_table').bootstrapTable({
            url: '/table_water_saving/management_select',         //请求后台的URL（*）
            method: 'get',                      //请求方式（*）
            toolbar: '#toolbar',                //工具按钮用哪个容器
            striped: false,                      //是否显示行间隔色
            cache: false,                       //是否使用缓存，默认为true，所以一般情况下需要设置一下这个属性（*）
            pagination: true,                   //是否显示分页（*）
            queryParams: oTableInit.queryParams,//传递参数（*）
            sidePagination: "client",           //分页方式：client客户端分页，server服务端分页（*）
            pageNumber: 1,                       //初始化加载第一页，默认第一页
            pageSize: 100,                       //每页的记录行数（*）
            pageList: [10, 25, 50, 100],        //可供选择的每页的行数（*）
            showColumns: true,                  //是否显示所有的列
            showRefresh: true,                  //是否显示刷新按钮
            clickToSelect: true,                //是否启用点击选中行
            uniqueId: "id",                     //每一行的唯一标识，一般为主键列
            columns: managementColumns,
            onLoadSuccess: function (data) {
                //添加表尾
                addManagementTableFoot();
                //修改注释
                getManagementNote();
                //初始化总分数据
                initTotalScore();
                var data = $('#management_table').bootstrapTable('getData', false);//获取全部数据
                // 初始化待合并的序号
                produceShowId(data, $('#management_table'), 'assessmentIndicator');
                // 合并表格
                mergeCells(data, 'assessmentIndicator', 1, $('#management_table'));
            },
            onEditableSave: function (field, row, oldValue, $el) {
                //如果输入值超过规定范围，给出提示
                judgeValue(field, row, oldValue);

            }
        });
    };

    //得到查询的参数
    oTableInit.queryParams = function (params) {
        var temp = {   //这里的键的名字和控制器的变量名必须一直，这边改动，控制器也需要改成一样的
            limit: params.limit,   //页面大小
            offset: params.offset,  //页码
            last_id: label_id,
        };
        return temp;
    };
    return oTableInit;
};

var TechnicalTableInit = function () {
    var oTableInit = new Object();
    //初始化Table
    oTableInit.Init = function () {
        var tableName = document.getElementById("technical_table").createCaption();
        tableName.innerHTML = "<b>技术考核指标</b>";
        $('#technical_table').bootstrapTable({
            url: '/table_water_saving/technical_select',         //请求后台的URL（*）
            method: 'get',                      //请求方式（*）
            //toolbar: '#toolbar',                //工具按钮用哪个容器
            striped: false,                      //是否显示行间隔色
            cache: false,                       //是否使用缓存，默认为true，所以一般情况下需要设置一下这个属性（*）
            pagination: true,                   //是否显示分页（*）
            queryParams: oTableInit.queryParams,//传递参数（*）
            sidePagination: "client",           //分页方式：client客户端分页，server服务端分页（*）
            pageNumber: 1,                       //初始化加载第一页，默认第一页
            pageSize: 100,                       //每页的记录行数（*）
            pageList: [10, 25, 50, 100],        //可供选择的每页的行数（*）
            showColumns: true,                  //是否显示所有的列
            showRefresh: true,                  //是否显示刷新按钮
            clickToSelect: true,                //是否启用点击选中行
            uniqueId: "id",                     //每一行的唯一标识，一般为主键列
            columns: technicalColumns,
            onLoadSuccess: function (data) {
                addTechnicalTableFoot();
                getTechnicalNote();
                var data = $('#technical_table').bootstrapTable('getData', false);//获取全部数据
                // 初始化待合并的序号
                produceShowId(data, $('#technical_table'), 'assessmentContent');
                //合并表格
                mergeCells(data, 'assessmentContent', 1, $('#technical_table'));
            },
        });
    };

    //得到查询的参数
    oTableInit.queryParams = function (params) {
        var temp = {   //这里的键的名字和控制器的变量名必须一直，这边改动，控制器也需要改成一样的
            limit: params.limit,   //页面大小
            offset: params.offset,  //页码
            last_id: label_id,
        };
        return temp;
    };
    return oTableInit;
};

//添加管理考核指标表尾
function addManagementTableFoot() {
    $('#management_tfoot').remove();
    $('#management_table').append('<tfoot id="management_tfoot">\
        <tr>\
            <th id="text" style="text-align: center; vertical-align: middle; " data-field=""><div class="th-inner">评价结果</div><div class="fht-cell"/></th>\
            <th id="assessment_result" style="text-align: center; vertical-align: middle; " data-field=""><div class="th-inner">达到/未达到节水型企业管理考核指标的要求</div><div class="fht-cell"/></th>\
            <th id="total_score" style="text-align: center; vertical-align: middle; " data-field=""><div class="th-inner">0</div><div class="fht-cell"/></th>\
        </tr>\
        <tr>\
            <td id="management_note" ></td >\
        </tr>\
    </tfoot>\
	');
    $('#text').attr('colspan', 2);
    $('#assessment_result').attr('colspan', 4);
    $('#total_score').attr('colspan', 1);
    $('#management_note').attr('colspan', 7);
}

//添加技术考核指标表尾
function addTechnicalTableFoot() {
    $('#technical_tfoot').remove();
    $('#technical_table').append('<tfoot id="technical_tfoot">\
        <tr>\
            <td id="technical_note" ></td >\
        </tr>\
    </tfoot>\
    ');
    $('#technical_note').attr('colspan', 7);
}

//判断输入值是否超出给定范围
function judgeValue(field, row, oldValue) {
    var value = row[field];
    var fullMark = row['fullMark'];
    if (value > fullMark || value < 0) {
        alert("请输入介于0~" + fullMark + "的值");
        //将单元格的值更新为原本的值
        $('#management_table').bootstrapTable('updateByUniqueId', {
            id: row.id,
            row: {
                score: oldValue,
            }
        });
        var data = $('#management_table').bootstrapTable('getData', false);//获取全部数据
        mergeCells(data, 'assessmentIndicator', 1, $('#management_table'));
    } else {    //输入值合法，更改表尾（总分，评价结果）
        updateTotalScore(row);
    }
}

//初始化总分和结果
function initTotalScore() {
    //更新总的总分
    var data = $('#management_table').bootstrapTable('getData', false);//获取全部数据
    var totalScore = parseFloat('0');
    for (var i = 0; i < data.length; i++) {
        if (data[i]['score'] != '') {
            totalScore += parseFloat(data[i]['score']);
        }
    }
    $("#total_score").text(totalScore);

    for (var i = 0; i < data.length; i++) {
        var assessmentIndicator = data[i]['assessmentIndicator'];
        var count = 0;
        var sum = parseFloat('0');
        for (var j = i; j < data.length; j++) {
            if (data[j]['assessmentIndicator'] == assessmentIndicator) {
                if (data[j]['score'] != '') {
                    sum += parseFloat(data[j]['score']);
                }
                count++;
            }
        }
        for (var j = i; j < data.length; j++) {
            if (data[j]['assessmentIndicator'] == assessmentIndicator) {
                $('#management_table').bootstrapTable('updateCell', {
                    index: j,
                    field: 'totalScore',
                    value: sum
                })
            }
        }
        i = i + count - 1;
    }
    getResult();
}

function getResult() {
    var totalScore = document.getElementById('total_score').innerHTML;  //总分
    var sum = 0;
    var data = $('#management_table').bootstrapTable('getData', false);//获取全部数据
    for (var i = 0; i < data.length; i++) {
        if (data[i]['assessmentIndicator'] == "管理制度" || data[i]['assessmentIndicator'] == "管理机构和人员" || data[i]['assessmentIndicator'] == "水计量管理" || data[i]['assessmentIndicator'] == "水平衡测试") {
            sum += parseFloat(data[i]['score']);
        }
    }
    if (totalScore >= 52 && sum >= 34) {
        $("#assessment_result").text("达到节水型企业管理考核指标的要求");
    } else {
        if (totalScore == 0) {
            $("#assessment_result").text("");
        } else {
            $("#assessment_result").text("未达到节水型企业管理考核指标的要求");
        }
    }
}

//每输入一个评分，更新总分和评价结果
function updateTotalScore(row) {
    //更新总的总分
    var data = $('#management_table').bootstrapTable('getData', false);//获取全部数据
    var totalScore = parseFloat('0');
    for (var i = 0; i < data.length; i++) {
        if (data[i]['score'] != "") {
            totalScore += parseFloat(data[i]['score']);
        }
    }
    $("#total_score").text(totalScore);

    //更新当前项总分
    //计算当前项总分
    var assessmentIndicator = row.assessmentIndicator;
    var sum = parseFloat('0');
    for (var i = 0; i < data.length; i++) {
        if (data[i]['assessmentIndicator'] == assessmentIndicator) {
            if (data[i]['score'] != '') {
                sum += parseFloat(data[i]['score']);
            }
        }
    }

    for (var i = 0; i < data.length; i++) {
        if (data[i]['assessmentIndicator'] == assessmentIndicator) {
            $('#management_table').bootstrapTable('updateCell', {
                index: i,
                field: 'totalScore',
                value: sum
            })
        }
    }
    //重新合并表格
    mergeCells(data, 'assessmentIndicator', 1, $('#management_table'));
    getResult();
}

// 初始加载时，产生待合并的序号
function produceShowId(data, target, fieldName) {
    var showId = 0;
    for (var i = 0; i < data.length; i++) {
        if (i == 0) {
            showId++;
        } else if (data[i][fieldName] != data[i - 1][fieldName]) {
            showId++;
        }
        $(target).bootstrapTable('updateCell', {
            index: i,
            field: 'showId',
            value: showId
        });
    }
}

//合并表格
function mergeCells(data, fieldName, colspan, target) {
    //声明一个map计算相同属性值在data对象出现的次数和
    var sortMap = {};
    for (var i = 0; i < data.length; i++) {
        for (var prop in data[i]) {
            if (prop == fieldName) {
                var key = data[i][prop]     //fieldName的value
                if (sortMap.hasOwnProperty(key)) {
                    sortMap[key] = sortMap[key] * 1 + 1;
                } else {
                    sortMap[key] = 1;
                }
                break;
            }
        }
    }
    //合并单元格
    var index = 0;
    for (var prop in sortMap) {
        var count = sortMap[prop] * 1;
        $(target).bootstrapTable('mergeCells', {index: index, field: fieldName, colspan: colspan, rowspan: count});
        //合并总分
        $(target).bootstrapTable('mergeCells', {index: index, field: 'totalScore', colspan: colspan, rowspan: count});
        //合并序号
        $(target).bootstrapTable('mergeCells', {index: index, field: 'showId', colspan: colspan, rowspan: count});
        index += count;
    }
}

//返回按钮
$('#btn_return').on('click', function () {
    $('#page-wrapper').load('pages/label_water_saving.html');
});

//提交表格
$('#submit').on('click', function () {
    //输入框不能为空
    var managementData = $('#management_table').bootstrapTable('getData', false);//获取全部数据
    for (var i = 0; i < managementData.length; i++) {
        if (managementData[i]['score'] == null || managementData[i]['score'] == '') {
            alert("管理考核指标的评分不能为空！");
            return;
        }
    }
    var technicalData = $('#technical_table').bootstrapTable('getData', false);//获取全部数据
    for (var i = 0; i < technicalData.length; i++) {
        if (technicalData[i]['value'] == null || technicalData[i]['value'] == '') {
            alert("技术考核指标的计算值不能为空！");
            return;
        }
    }
    var flag = 1;   //两个表都成功插入，更新label_water_saving
    //更新management_indicator表
    for (var i = 0; i < managementData.length; i++) {
        $.ajax({
            type: "post",
            url: "/table_water_saving/management_update",
            async: false,
            data: {
                'id': managementData[i]['id'],
                'score': managementData[i]['score']
            },
            error: function () {
                flag = 0;
                bootoast({
                    message: '通讯异常',
                    position: 'top',
                    type: 'danger',
                    timeout: 1.5,
                });
            }
        });
    }

    //更新technical_indicator_table表
    for (var i = 0; i < technicalData.length; i++) {
        var input1;
        var input2;
        var input3;
        var formulaList;
        if (label_eva_basis == 'GB/T 26923-2011') formulaList = formulaList2011;
        else if (label_eva_basis == 'GB/T 7119-2018') formulaList = formulaList2018;
        for (var j = 0; j < formulaList.length; j++) {
            if (formulaList[j].technicalIndicator == technicalData[i].technicalIndicator) {
                input1 = formulaList[j].input1;
                input2 = formulaList[j].input2;
                input3 = formulaList[j].input3;
                break;
            }
        }

        $.ajax({
            type: "post",
            url: "/table_water_saving/technical_update",
            async: false,
            data: {
                'id': technicalData[i]['id'],
                'value': technicalData[i]['value'],
                'input1': input1,
                'input2': input2,
                'input3': input3
            },
            error: function () {
                flag = 0;
                bootoast({
                    message: '通讯异常',
                    position: 'top',
                    type: 'danger',
                    timeout: 1.5,
                });
            }
        });
    }
    //都成功插入,修改对应的label_water_saving的状态
    if (flag == 1) {
        $.ajax({
            type: "post",
            url: "/table_water_saving/label_update",
            async: false,
            data: {
                'id': label_id,
                'state': "已完成",
                'modifyTime': new Date()
            },
            success: function (data) {
                if (data['status'] == "success") {
                    $('#page-wrapper').load('pages/label_water_saving.html');
                }
            },
            error: function () {
                bootoast({
                    message: '通讯异常',
                    position: 'top',
                    type: 'danger',
                    timeout: 1.5,
                });
            }
        });
    } else {
        alert("插入失败");
        return;
    }
});

//保存表格
$('#save').on('click', function () {
    var flag = 1;   //两个表都成功插入，更新label_water_saving
    //更新management_indicator表
    var managementData = $('#management_table').bootstrapTable('getData', false);//获取全部数据
    for (var i = 0; i < managementData.length; i++) {
        $.ajax({
            type: "post",
            url: "/table_water_saving/management_update",
            async: false,
            data: {
                'id': managementData[i]['id'],
                'score': managementData[i]['score'],
            },
            error: function () {
                flag = 0;
                bootoast({
                    message: '通讯异常',
                    position: 'top',
                    type: 'danger',
                    timeout: 1.5,
                });
            }
        });
    }

    //更新technical_indicator_table表
    var technicalData = $('#technical_table').bootstrapTable('getData', false);//获取全部数据
    for (var i = 0; i < technicalData.length; i++) {
        var input1;
        var input2;
        var input3;
        var formulaList;
        if (label_eva_basis == 'GB/T 26923-2011') formulaList = formulaList2011;
        else if (label_eva_basis == 'GB/T 7119-2018') formulaList = formulaList2018;
        for (var j = 0; j < formulaList.length; j++) {
            if (formulaList[j].technicalIndicator == technicalData[i].technicalIndicator) {
                input1 = formulaList[j].input1;
                input2 = formulaList[j].input2;
                input3 = formulaList[j].input3;
                break;
            }
        }

        $.ajax({
            type: "post",
            url: "/table_water_saving/technical_update",
            async: false,
            data: {
                'id': technicalData[i]['id'],
                'value': technicalData[i]['value'],
                'input1': input1,
                'input2': input2,
                'input3': input3
            },
            error: function () {
                flag = 0;
                bootoast({
                    message: '通讯异常',
                    position: 'top',
                    type: 'danger',
                    timeout: 1.5,
                });
            }
        });
    }
    //都成功插入,修改对应的label_water_saving的状态
    if (flag == 1) {
        bootoast({
            message: '保存成功',
            position: 'top',
            type: 'success',
            timeout: 2,
        });

        $.ajax({
            type: "post",
            url: "/table_water_saving/label_update",
            async: false,
            data: {
                'id': label_id,
                'state': "填写中",
                'modifyTime': new Date()
            },
            error: function () {
                bootoast({
                    message: '通讯异常',
                    position: 'top',
                    type: 'danger',
                    timeout: 1.5,
                });
            }
        });
    } else {
        alert("保存失败");
        return;
    }
});

//取消提交表格
$('#cancel').on('click', function () {
    //点击取消按钮，返回到label页，将状态改为填写中
    $.ajax({
        type: "post",
        url: "/table_water_saving/label_update",
        async: false,
        data: {
            'id': label_id,
            'state': "填写中",
            'modifyTime': new Date()
        },
        success: function (data) {
            if (data['status'] == "success") {
                $('#page-wrapper').load('pages/label_water_saving.html');
            }
        },
        error: function () {
            bootoast({
                message: '通讯异常',
                position: 'top',
                type: 'danger',
                timeout: 1.5,
            });
        }
    });
});

function getManagementNote() {
    var num = 1;
    $.ajax({
        url: "/table_water_saving/getManagementNote",
        type: "get",
        async: false,
        data: 'standardNumber=' + label_eva_basis,
        success: function (result) {
            //var ele = document.getElementById("management_note");
            var list = result['rows'];
            for (var i = 0; i < list.length; i++) {
                var note = document.getElementById("management_note");
                note.innerHTML = note.innerHTML + "注" + num + ": " + list[i].note + "<br />";
                num++;
            }
            if (list.length == 0) {
                var note = document.getElementById("management_note");
                note.innerHTML = "无注释";
            }
        },
        error: function () {
            bootoast({
                message: '通讯异常',
                position: 'top',
                type: 'danger',
                timeout: 1.5,
            });
        }
    });
}

function getTechnicalNote() {
    var num = 1;
    $.ajax({
        url: "/table_water_saving/getTechnicalNote",
        type: "get",
        async: false,
        data: 'standardNumber=' + label_eva_basis,
        success: function (result) {
            //var ele = document.getElementById("management_note");
            var list = result['rows'];
            for (var i = 0; i < list.length; i++) {
                var note = document.getElementById("technical_note");
                note.innerHTML = note.innerHTML + "注" + num + ": " + list[i].note + "<br />";
                num++;
            }
            if (list.length == 0) {
                var note = document.getElementById("technical_note");
                note.innerHTML = "无注释";
            }
        },
        error: function () {
            bootoast({
                message: '通讯异常',
                position: 'top',
                type: 'danger',
                timeout: 1.5,
            });
        }
    });
}

function initFormulaList() {
    var formulaList;
    if (label_eva_basis == 'GB/T 26923-2011') formulaList = formulaList2011;
    else if (label_eva_basis == 'GB/T 7119-2018') formulaList = formulaList2018;
    //获取技术考核指标内容
    $.ajax({
        url: "/table_water_saving/technical_select",
        type: "get",
        async: false,
        data: {last_id: label_id},
        success: function (result) {
            var list = result['rows'];
            for (var i = 0; i < list.length; i++) {
                for (var j = 0; j < formulaList.length; j++) {
                    if (list[i].technicalIndicator == formulaList[j].technicalIndicator) {
                        if (list[i].input1 != -1) formulaList[j].input1 = list[i].input1;
                        if (list[i].input2 != -1) formulaList[j].input2 = list[i].input2;
                        if (list[i].input3 != -1) formulaList[j].input3 = list[i].input3;
                        break;
                    }
                }
            }
        },
        error: function () {
            bootoast({
                message: '通讯异常',
                position: 'top',
                type: 'danger',
                timeout: 1.5,
            });
        }
    });
}


$(function () {
    if (operation == 'view') {
        managementColumns[6]['editable']['disabled'] = true;
        $('#save').hide();
        $('#submit').hide();
        $('#cancel').hide();

        //初始化管理考核指标表
        var managementTable = new ManagementTableInit();
        managementTable.Init();

        //初始化技术考核指标表
        var technicalTable = new TechnicalTableInit();
        technicalTable.Init();
    } else if (operation == 'export') {
        managementColumns[6]['editable']['disabled'] = true;
        $('#save').hide();
        $('#submit').hide();
        $('#cancel').hide();
        $('#btn_return').hide();

        //初始化管理考核指标表
        var managementTable = new ManagementTableInit();
        managementTable.Init();

        //初始化技术考核指标表
        var technicalTable = new TechnicalTableInit();
        technicalTable.Init();

        bootbox.confirm({
            message: "确认导出？",
            locale: 'zh_CN',
            title: '操作提示',
            callback: function (result) {
                if (result) {
                    $("#management_table").tableExport({fileName: '管理考核指标', type: "xls", escape: "false"});
                    $("#technical_table").tableExport({fileName: '技术考核指标', type: "xls", escape: "false"});
                    $('#page-wrapper').load("pages/label_water_saving.html");
                } else {
                    $('#page-wrapper').load("pages/label_water_saving.html");
                }
            }
        });
    } else if (operation == 'edit') {
        //初始化管理考核指标表
        var managementTable = new ManagementTableInit();
        managementTable.Init();

        //初始化技术考核指标表
        var technicalTable = new TechnicalTableInit();
        technicalTable.Init();

        //将模态框输入数据导入formulaList
        initFormulaList();
        //引入Mathjax
        var script = document.createElement('script');
        script.src = 'https://cdn.jsdelivr.net/npm/mathjax@3/es5/tex-chtml.js';
        script.async = true;
        document.head.appendChild(script);
    }
    //3.隐藏列
    $('#management_table').bootstrapTable('hideColumn', 'id');
    $('#technical_table').bootstrapTable('hideColumn', 'id');
});

var index;
var indicator;

function edit(showId, technicalIndicator) {
    $('#input1').val('');
    $('#input2').val('');
    $('#input3').val('');
    index = showId;
    indicator = technicalIndicator;
    var title = document.getElementById('title');
    var label1 = document.getElementById('label1');
    var label2 = document.getElementById('label2');
    var label3 = document.getElementById('label3');
    var formulaList;
    if (label_eva_basis == 'GB/T 7119-2018') formulaList = formulaList2018;
    else if (label_eva_basis == 'GB/T 26923-2011') formulaList = formulaList2011;

    //初始化模态框已输入数据
    for (var i = 0; i < formulaList.length; i++) {
        if (formulaList[i].technicalIndicator == technicalIndicator) {
            if (formulaList[i].input1 != -1) $('#input1').val(formulaList[i].input1);
            if (formulaList[i].input2 != -1) $('#input2').val(formulaList[i].input2);
            if (formulaList[i].input3 != -1) $('#input3').val(formulaList[i].input3);
            break;
        }
    }

    for (var e in formulaList) {
        if (technicalIndicator == formulaList[e].technicalIndicator) {
            title.innerHTML = formulaList[e].formula;
            label1.innerHTML = formulaList[e].label1;
            label2.innerHTML = formulaList[e].label2;
            label3.innerHTML = formulaList[e].label3;
            if (formulaList[e].label3 == "") {
                $("#label3").attr("style", "display:none");
                $("#input3").attr("style", "display:none");
            } else {
                $("#label3").attr("style", "display:block");
                $("#input3").attr("style", "display:block");
            }
        }
    }
    $('#formula').modal();
    MathJax.typeset();
}

$('#formula_submit').on('click', function () {
    var input1 = parseFloat($('#input1').val());
    var input2 = parseFloat($('#input2').val());
    var input3;
    if ($('#input3').val() == '') input3 = -1;
    else input3 = parseFloat($('#input3').val());
    var value;
    if (label_eva_basis == 'GB/T 7119-2018') {
        if (indicator == "单位产品取水量") value = (input1 / input2).toFixed(2);
        else if (indicator == "重复利用率") value = (input1 / (input1 + input2) * 100).toFixed(2);
        else if (indicator == "直接冷却水循环率") value = (input1 / (input1 + input2) * 100).toFixed(2);
        else if (indicator == "间接冷却水循环率") value = (input1 / (input1 + input2) * 100).toFixed(2);
        else if (indicator == "蒸汽冷凝水回用率") value = (input1 / input2 * input3 * 100).toFixed(2);
        else if (indicator == "废水回用率") value = (input1 / (input1 + input2) * 100).toFixed(2);
        else if (indicator == "非常规水源替代率") value = (input1 / (input1 + input2) * 100).toFixed(2);
        else if (indicator == "非常规水源利用率") value = (input1 / input2 * 100).toFixed(2);
        else if (indicator == "用水综合漏失率") value = (input1 / input2 * 100).toFixed(2);
        else if (indicator == "达标排放率") value = (input1 / input2 * 100).toFixed(2);
        else if (indicator == "水表计量率") value = (input1 / input2 * 100).toFixed(2);
        else if (indicator == "化学水制取系数") value = (input1 / input2).toFixed(2);
        else if (indicator == "循环水浓缩倍数") value = (input1 / input2).toFixed(2);
        else if (indicator == "蒸汽冷凝水回收率") value = (input1 / input2 * input3 * 100).toFixed(2);
        else if (indicator == "单位产品排水量") value = (input1 / input2).toFixed(2);
        else alert("此技术指标无对应公式");
    } else if (label_eva_basis == 'GB/T 26923-2011') {
        if (indicator == "棉、麻、化纤及混纺机织物") value = (input1 / input2).toFixed(2);
        else if (indicator == "丝绸机织物") value = (input1 / input2).toFixed(2);
        else if (indicator == "针织物及纱线") value = (input1 / input2).toFixed(2);
        else if (indicator == "重复利用率") value = (input1 / (input1 + input2 + input3) * 100).toFixed(2);
        else if (indicator == "间接冷却水循环率") value = (input1 / (input1 + input2) * 100).toFixed(2);
        else if (indicator == "冷凝水回用率") value = (input1 / input2 * input3 * 100).toFixed(2);
        else if (indicator == "废水回用率") value = (input1 / (input1 + input2) * 100).toFixed(2);
        else if (indicator == "用水综合漏失率") value = (input1 / input2 * 100).toFixed(2);
        else alert("此技术指标无对应公式");
    }

    //更新输入的值
    var formulaList;
    if (label_eva_basis == 'GB/T 7119-2018') formulaList = formulaList2018;
    else if (label_eva_basis == 'GB/T 26923-2011') formulaList = formulaList2011;
    for (var e in formulaList) {
        if (indicator == formulaList[e].technicalIndicator) {
            formulaList[e].input1 = input1;
            formulaList[e].input2 = input2;
            formulaList[e].input3 = input3;
            break;
        }
    }

    $('#technical_table').bootstrapTable('updateCell', {
        index: index,
        field: 'value',
        value: value
    })

    $('#formula').modal('hide');
    var data = $('#technical_table').bootstrapTable('getData', false);//获取全部数据
    mergeCells(data, 'assessmentContent', 1, $('#technical_table'));
});